#include "Main.h"

byte* cbagPtr;
byte* GetItemIdByIndexFunctionPtr;
byte* UseItemFunctionPtr;

void CBag::Init()
{
	__try {
		cbagPtr = (byte*)(*(dword*)Mem->GetAddress("CBagPtr"));
		GetItemIdByIndexFunctionPtr = Mem->GetAddress("GetItemIdByIndexFunction");
		UseItemFunctionPtr = Mem->GetAddress("UseItemFunction");
	}
	__except(1) { }
}

unsigned long CBag::GetItemIdByIndex(unsigned long index)
{
	unsigned long retVal;

	if(!cbagPtr || !GetItemIdByIndexFunctionPtr)
		return NULL;

	_asm {
		PUSH index
		MOV ECX,cbagPtr
		CALL GetItemIdByIndexFunctionPtr
		MOV retVal,EAX
	}

	return retVal;
}

void CBag::UseItem(unsigned long index)
{
	UseItemWrapper(index);
}

void CBag::UseItemByItemId(unsigned long itemId)
{
	if(!cbagPtr || !UseItemFunctionPtr)
		return;

	dword item;
	for(int i = 0;; i++)
	{
		item = GetItemIdByIndex(i);
		if(item == SlotLocked)
			break;

		if(item == itemId)
		{
			UseItem(i);
			break;
		}
	}
}